package cn.bdqn.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.bdqn.pojo.Project;
import cn.bdqn.pojo.Workorder;
import cn.bdqn.service.ProjectService;
import cn.bdqn.service.StaffService;
import cn.bdqn.service.WorkorderService;

@Controller
public class WorkorderController {
        @Resource(name = "projectService")
        private ProjectService pjService;

        @Resource(name = "workorderService")
        private WorkorderService wkodService;

        @Resource(name = "staffService")
        private StaffService stfservice;
        
        // 登陆
        @RequestMapping("login.do")
        @ResponseBody
        public JSONObject loginIn(HttpSession session,@RequestParam String user,@RequestParam String pwd) {
//      public String loginIn(HttpSession session,@RequestParam String user,@RequestParam String pwd) {  
//        	System.out.println("用户名："+user+"\t密码："+pwd);
        	String name = stfservice.querystaff(user, pwd);
        	System.out.println("查询结果"+name);
        	JSONObject json=new JSONObject();
        	session.setAttribute("username",name);
        	json.put("name", name);
        	System.out.println("设置的结果是："+json);
        	return json;
//        	return "wpfscc!";
        }

        // 注册
        @RequestMapping("register.do")
        @ResponseBody
        public Object registerIn(HttpSession session,@RequestParam String user,@RequestParam String pwd) {
        	System.out.println("用户名："+user+"\t密码："+pwd);
//        	String name = stfservice.addStaff(Staff.class);	添加用户
//        	session.setAttribute("username",name);
        	JSONObject json=new JSONObject();
//        	json.put("name", name);
        	return json;
        }
        // 退出
        @RequestMapping("exit.do")
        public String exitIn(HttpSession session, HttpServletRequest rq) {
        	rq.getSession().invalidate();
        	rq.getSession().removeAttribute("user");
        	return "exitSeccess";
        }
        // 首页项目查询
        @RequestMapping("/")
        public String homePage(Model model) {
                List<Project> plist = pjService.queryPList();
                model.addAttribute("plist", plist);
                return "homePage";
//                return "homePage";
        }
        
        // 工单列表
        @RequestMapping("orderList.html")
        public String orderList(Model model) {
                List<Workorder> wList = wkodService.queryWList();
                model.addAttribute("wlist", wList);
                return "orderList";
        }

        // 进入添加工单界面
        @RequestMapping("addOrder.html")
        public String addOrderView(@RequestParam("projectId") int projectId,
                        Model model) {
                model.addAttribute("projectId", projectId);
                System.out.println("后台接受url原始参数" + projectId);
                return "addOrder";
        }

        // 提交添加工单的表单

        @RequestMapping("addOrderSave.html")
        public String addOrder(Model model, HttpServletRequest rq,
                        @RequestParam("oexecutor") String executor,
                        @RequestParam("odescription") String description,
                        @RequestParam("olevel") Long orderLevel,
                        @RequestParam("pid") Long pid) {
                Workorder workorder = new Workorder();
                Date date = new Date();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                workorder.setExecutor(executor);
                workorder.setDescription(description);
                workorder.setOrderLevel(orderLevel);
                workorder.setProjectId(pid);
                workorder.setCreateDate(formatter.format(date));
                wkodService.addWorkorder(workorder);
                return "forward:orderList.html";
        }
}
